package com.hnucm.cooklife.presenter.me;

import android.icu.number.Scale;

import androidx.annotation.NonNull;

import com.hnucm.cooklife.base.BasePresenter;
import com.hnucm.cooklife.bean.me.DishColl;
import com.hnucm.cooklife.contract.realDish.Contract;
import com.hnucm.cooklife.model.RealDishModel;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class RealDishPresenter extends BasePresenter<Contract.IReadDishView> implements Contract.IRealDishPresenter {

    Contract.IRealDishModel mIRealDishModel;
    public RealDishPresenter() {
        mIRealDishModel = new RealDishModel();
    }

    @Override
    public void loadRealMyDish(int userid) {
        mIRealDishModel.loadRealMyDish(userid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<DishColl>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull List<DishColl> dishCollList) {
                        if(isViewAttached()){
                            getView().onLoadRealMyDish(dishCollList);
//                            System.out.println(dishCollList);
                            getView().onLoadSuccess();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        System.out.println(e.getMessage());
                        if(isViewAttached()){
                            getView().onLoadingFailed();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void refreshRealMyDish(int userid) {

    }
}
